package com.air.ota.service.imp;

import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.air.common.service.imp.BaseServiceImp;
import com.air.common.util.QueryCondition;
import com.air.common.util.QueryCondition4Update;
import com.air.common.util.QueryExpression;
import com.air.ota.dao.CardPwdDao;
import com.air.ota.mo.CardPwd;
import com.air.ota.service.ICardPwdService;

@Service("cardPwdService")
public class CardPwdServiceImp extends BaseServiceImp implements
		ICardPwdService, InitializingBean {

	@Autowired
	CardPwdDao cardPwdMapper;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		this.baseDao = cardPwdMapper;
	}
	

	@Override
	public boolean auth(String cellphone, String password) {
		
		QueryCondition condition = new QueryCondition();
		condition.addQueryCondition("cellphone", cellphone, QueryExpression.EQUALS);
		condition.addQueryCondition("password", password, QueryExpression.EQUALS);
		List<CardPwd> result=this.queryByCondition(condition);
		if (result==null||result.size()==0){
			return false;
		}
		return true;
	}
	
	@Override
	public boolean resetPwd(String cellphone){
		QueryCondition condition = new QueryCondition();
		condition.addQueryCondition("cellphone", cellphone, QueryExpression.EQUALS);
		
		List<CardPwd> result=this.queryByCondition(condition);
		if (result==null||result.size()==0){
			return false;
		}
		CardPwd record = result.get(0);
		record.setPassword(record.getInitPwd());
		this.updateById(record);
		return true;
	}


	@Override
	public boolean setPwd(String cellphone, String newPwd) {
		QueryCondition4Update condition4Update = new QueryCondition4Update();
		condition4Update.addQueryCondition("cellphone", cellphone, QueryExpression.EQUALS);
		condition4Update.addUpdateFieldExpression("password", newPwd);
		int resultCount=this.updateByCondition(condition4Update);
		// TODO Auto-generated method stub
		
		return true;
	}
	
	
}
